package com.pearadmin.modules.blog.controller;


import com.pearadmin.modules.blog.domain.Article;
import com.pearadmin.modules.blog.service.IArticleService;


import com.github.pagehelper.PageInfo;
import com.pearadmin.common.tools.StringUtils;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.ResultTable;
import com.pearadmin.modules.blog.service.IClassificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.List;


@RestController
@Api(tags = "文章")
@RequestMapping("/blog/article")
public class ArticleController extends BaseController {

    /**
         * Describe: 基础路径
         * */
    private static String MODULE_PATH = "blog/article/";

    @Resource
    IClassificationService classificationService;

    @Resource
    private IArticleService articleService;

    @GetMapping("main")
    @ApiOperation(value="获取列表视图")
    @PreAuthorize("hasPermission('/blog/article/main','blog:article:main')")
    public ModelAndView main( ){
        return JumpPage(MODULE_PATH + "main");
    }


    @GetMapping("data")
    @ApiOperation(value="获取角色列表数据")
    @PreAuthorize("hasPermission('/blog/article/main','blog:article:main')")
    public ResultTable data(PageDomain pageDomain, Article param){
        PageInfo<Article> pageInfo = articleService.page(param,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    @GetMapping("add")
    @ApiOperation(value="获取新增视图")
    @PreAuthorize("hasPermission('/blog/article/add','blog:article:add')")
    public ModelAndView add(ModelMap map){
        List list = classificationService.selectList(null);
        map.put("list",list);
        return JumpPage(MODULE_PATH + "add");
    }


    /**
     * Describe: 保存信息
     * Param SysRole
     * Return 执行结果
     * */
    @PostMapping("save")
    @ApiOperation(value="保存数据")
    @PreAuthorize("hasPermission('/blog/article/add','blog:article:add')")
    public Result save(@RequestBody Article article){
        int result = articleService.insert(article);
        return decide(result);
    }

    /**
     * Describe: 获取修改视图
     * Param ModelAndView
     * Return 修改视图
     * */
    @GetMapping("edit")
    @ApiOperation(value="获取修改视图")
    @PreAuthorize("hasPermission('/blog/article/edit','blog:article:edit')")
    public ModelAndView edit(ModelAndView modelAndView,int id){
        modelAndView.addObject("article",articleService.selectById(id));
        modelAndView.setViewName(MODULE_PATH + "edit");
        return modelAndView;
    }

    /**
     * Describe: 修改信息
     * Param SysRole
     * Return 执行结果
     * */
    @PutMapping("update")
    @ApiOperation(value="修改数据")
    @PreAuthorize("hasPermission('/blog/article/edit','blog:article:edit')")
    public Result update(@RequestBody Article article){
        int result = articleService.updateById(article);
        return decide(result);
    }

    /**
     * Describe: 用户删除接口
     * Param: id
     * Return: ResuBean
     * */
    @DeleteMapping("remove/{id}")
    @ApiOperation(value="删除数据")
    @PreAuthorize("hasPermission('/blog/article/remove','blog:article:remove')")
    public Result remove(@PathVariable int id){
        int result  = articleService.deleteById(id);
        return decide(result);
    }

    /**
     * Describe: 用户批量删除接口
     * Param: ids
     * Return: ResuBean
     * */
    @DeleteMapping("batchRemove/{ids}")
    @ApiOperation(value="批量删除数据")
    @PreAuthorize("hasPermission('/blog/article/remove','blog:article:remove')")
    public Result batchRemove(@PathVariable String ids){
        int result = articleService.deleteByIds(StringUtils.StringToIntArr(ids.split(",")).toArray());
        return decide(1);
    }


}
